﻿using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows;

namespace Oxage.CNC.Models
{
	public class XamlPathDataCodeCubicBezierCurveInline : XamlPathDataCodeInline
	{
		#region Constructors
		public XamlPathDataCodeCubicBezierCurveInline()
		{
		}

		public XamlPathDataCodeCubicBezierCurveInline(string inline)
		{
			Parse(inline);
		}
		#endregion

		#region Public properties
		public Point ControlPoint1
		{
			get;
			set;
		}

		public Point ControlPoint2
		{
			get;
			set;
		}

		public Point EndPoint
		{
			get;
			set;
		}
		#endregion

		#region Public methods
		public override void Parse(string inline)
		{
			var match = Regex.Match(inline, @"(?<code>\w)\s*(?<x1>[\d\-\+\.]+)\s*,\s*(?<y1>[\d\-\+\.]+)\s+(?<x2>[\d\-\+\.]+)\s*,\s*(?<y2>[\d\-\+\.]+)\s+(?<x3>[\d\-\+\.]+)\s*,\s*(?<y3>[\d\-\+\.]+)", RegexOptions.IgnoreCase);
			if (match.Success)
			{
				this.Code = match.Groups["code"].Value[0];
				double x1 = double.Parse(match.Groups["x1"].Value, CultureInfo.InvariantCulture);
				double y1 = double.Parse(match.Groups["y1"].Value, CultureInfo.InvariantCulture);
				double x2 = double.Parse(match.Groups["x2"].Value, CultureInfo.InvariantCulture);
				double y2 = double.Parse(match.Groups["y2"].Value, CultureInfo.InvariantCulture);
				double x3 = double.Parse(match.Groups["x3"].Value, CultureInfo.InvariantCulture);
				double y3 = double.Parse(match.Groups["y3"].Value, CultureInfo.InvariantCulture);
				this.ControlPoint1 = new Point(x1, y1);
				this.ControlPoint2 = new Point(x2, y2);
				this.EndPoint = new Point(x3, y3);
			}
		}

		public override string ToString()
		{
			string x1 = this.ControlPoint1.X.ToString(CultureInfo.InvariantCulture);
			string y1 = this.ControlPoint1.Y.ToString(CultureInfo.InvariantCulture);
			string x2 = this.ControlPoint2.X.ToString(CultureInfo.InvariantCulture);
			string y2 = this.ControlPoint2.Y.ToString(CultureInfo.InvariantCulture);
			string x3 = this.EndPoint.X.ToString(CultureInfo.InvariantCulture);
			string y3 = this.EndPoint.Y.ToString(CultureInfo.InvariantCulture);
			return string.Format("C{0},{1} {2},{3} {4},{5}", x1, y1, x2, y2, x3, y3);
		}
		#endregion
	}
}
